﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Grishko.UIControls
{
    [ToolboxData("<{0}:CellButton runat=server></{0}:CellButton >"),
    DefaultProperty("Text")]
    public class CellButton : WebControl, IPostBackDataHandler
    {
        public event EventHandler Click;
        public virtual void onClick(EventArgs arg)
        {
            if (null != Click)
                Click(this, arg);
        }

        const string OnOffStateKey = "OnOffStateKey";
        public CellButton()
        {


        }

        public string HighlightCssClass
        {
            get
            {
                return (string)ViewState["HighlightCssClass"];
            }
            set
            {
                ViewState["HighlightCssClass"] = value;
            }
        }

        public string NoHighlightCssClass
        {
            get
            {
                return (string)ViewState["NoHighlightCssClass"];
            }
            set
            {
                ViewState["NoHighlightCssClass"] = value;
            }
        }

        public string Text
        {
            get
            {
                return (string)ViewState["Text"];
            }
            set
            {
                ViewState["Text"] = value;
            }
        }

        public bool  Push
        {

            get
            {
                object obj = ViewState[OnOffStateKey];
                if (null == obj)
                    return false ;
                else
                    return Convert.ToBoolean(obj);
            }
            set
            {
                ViewState[OnOffStateKey] = value;
            }
        }
                

        private string PnlID { get { return "pnl" + this.UniqueID; } }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);          
        }

        protected override void Render(HtmlTextWriter writer)
        {
            Page.VerifyRenderingInServerForm(this);
            Page.ClientScript.GetPostBackEventReference(this, "");

            // регистрируем носитель
            Panel Carrier = new Panel() { Width = this.Width , Height = this .Height, ID = PnlID };
            //Если есть текст у кнопки - добавляем носитель текста
            if (string.IsNullOrEmpty(Text))
            {
                Label lb = new Label() { Text = Text };
                Carrier.Controls.Add(lb);
            }

            //Вставляем Input  дабы жил ipostback
            HiddenField hf = new HiddenField();
            hf.ID =this.UniqueID;        
            Carrier.Controls.Add(hf);
            

            Carrier.CssClass = (Push ? HighlightCssClass : this.NoHighlightCssClass );
            if (!Push)
            {
                Carrier.Attributes.Add("onmouseover", " var v = document.getElementById('" + PnlID.Replace ("$","_") + "');   v.className = '" + HighlightCssClass + "';");
                Carrier.Attributes.Add("onmouseout", " var v = document.getElementById('" + PnlID.Replace("$", "_") + "');  v.className = '" + NoHighlightCssClass + "';");
            }
            Carrier.Attributes.Add("onClick", "__doPostBack('" + this.ClientID + "', '"+this.ClientID+"');");
            Carrier.RenderControl(writer);
        }

        #region IPostBackDataHandler Members


        public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
        {
            return (postCollection["__EVENTARGUMENT"] == this.UniqueID);            
        }

        public void RaisePostDataChangedEvent()
        {
            Push=(!Push);
            onClick(new EventArgs());
        }

        #endregion
    }
}
